package com.zsj.util;

import com.zsj.enums.ErrorCodeEnum;
import com.zsj.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author:
 * @Date:
 */
public class MoneyUtil {

    private static final Pattern NUMERIC_PATTERN = Pattern.compile("^[0-9]+$");

    public static boolean isNumeric(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Matcher matcher = NUMERIC_PATTERN.matcher(str);
        return matcher.matches();
    }

    public static String fenToYuan(String fenStr) {
        if (StringUtils.isBlank(fenStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        long fen = Long.parseLong(fenStr);
        double yuan = fen / 100.0;
        return String.format("%.2f", yuan);
    }

    public static String yuanToHao(String yuanStr) throws NumberFormatException {
        if (StringUtils.isBlank(yuanStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        BigDecimal yuan = new BigDecimal(yuanStr);
        BigDecimal hao = yuan.multiply(new BigDecimal(10000));
        return hao.setScale(0, RoundingMode.HALF_UP).toString();
    }

    public static String yuanToFen(String yuanStr) throws NumberFormatException {
        if (StringUtils.isBlank(yuanStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        BigDecimal yuan = new BigDecimal(yuanStr);
        BigDecimal hao = yuan.multiply(new BigDecimal(100));
        return hao.setScale(0, RoundingMode.HALF_UP).toString();
    }

    public static String haoToYuan(String haoStr) {
        if (StringUtils.isBlank(haoStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        BigDecimal haoBigDecimal = new BigDecimal(haoStr);
        BigDecimal yuanBigDecimal = haoBigDecimal.divide(new BigDecimal(10000));
        return yuanBigDecimal.setScale(2, RoundingMode.HALF_UP).toString();
    }

    public static String haoToFen(String haoStr) {
        if (StringUtils.isBlank(haoStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        BigDecimal haoBigDecimal = new BigDecimal(haoStr);
        BigDecimal fenBigDecimal = haoBigDecimal.divide(new BigDecimal(100));
        return fenBigDecimal.setScale(2, RoundingMode.HALF_UP).toString();
    }

    public static String fenToHao(String fenStr) throws NumberFormatException {
        if (StringUtils.isBlank(fenStr)) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
        BigDecimal fen = new BigDecimal(fenStr);
        BigDecimal hao = fen.multiply(new BigDecimal(100));
        return hao.setScale(0, RoundingMode.HALF_UP).toString();
    }

    public static String discountedAmt(String amt, BigDecimal discount) {
        try {
            BigDecimal oriAmt = new BigDecimal(amt);
            BigDecimal discountedAmt = oriAmt.multiply(discount);
            return discountedAmt.setScale(0, RoundingMode.HALF_UP).toString();
        } catch (NumberFormatException e) {
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
        }
    }

    public static void main(String[] args) {
        System.out.println(fenToYuan("10000"));
        System.out.println(yuanToHao("2.2"));
        System.out.println(haoToFen("3001"));
        System.out.println(haoToYuan("300001"));
    }
}
